using Protocol.Dto;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LeftStatePanel : StatePanel
{
    public object MatchRoom { get; private set; }

    //fix bug
    protected override void Awake()
    {
        base.Awake();

        Bind(UIEvent.SET_LEFT_PLAYER_DATA,
            UIEvent.PLAYER_HIDE_STATE,
             UIEvent.PLAYER_LEAVE,
            UIEvent.PLAYER_ENTER,
            UIEvent.PLAYER_CHAT,
            UIEvent.PLAYER_READY);
    }

    public override void Execute(int eventCode, object message)
    {
        base.Execute(eventCode, message);

        switch (eventCode)
        {
            case UIEvent.PLAYER_CHAT:
                {
                    if (userDto == null)
                        break;
                    ChatMsg msg = message as ChatMsg;
                    if (userDto.Id == msg.UserId)
                        showChat(msg.Text);
                    break;
                }
            case UIEvent.PLAYER_HIDE_STATE:
                {
                    txtReady.gameObject.SetActive(false);
                    break;
                }
            case UIEvent.SET_LEFT_PLAYER_DATA:
                this.userDto = message as UserDto;
                break;
            case UIEvent.PLAYER_READY:
                base.readyState();
                break;
            case UIEvent.PLAYER_ENTER:
                {
                    if (userDto == null)
                        break;
                    int userId = (int)message;
                    if (userDto.Id == userId)
                    {
                        setPanelActive(true);
                        txtReady.gameObject.SetActive(false);
                    }
                    break;
                }
            case UIEvent.PLAYER_LEAVE:
                {
                    if (userDto == null)
                        break;
                    int userId = (int)message;
                    if (userDto.Id == userId)
                        setPanelActive(false);
                    break;
                }
            default:
                break;
        }
    }


    protected override void Start()
    {
        base.Start();

        //fix bug
        //如果！= -1 就代表 有角色
        MatchRoomDto room = Models.GameModel.MatchRoomDto;
        int leftId = room.LeftId;
        if (leftId != -1)
        {
            this.userDto = room.UIdUserDict[leftId];
            if(room.ReadyUIdList.Contains(leftId))
            {
                readyState();
            }
        }
        else
        {
            setPanelActive(false);
        }
    }

}
